<template>
  <div id="teamName">
    <van-dialog v-model="show" show-cancel-button cancel-button-text="下次再说" @cancel="cancelTap" @confirm="confirmTap">
      <p>{{ member ? "修改团队名称" : info_basic_member_team }}</p>
      <van-field v-model="nameVal" placeholder="请输入团队名称" />
    </van-dialog>
  </div>
</template>
<script>
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    member: {
      type: Boolean,
      default: false
    },
    info_basic_member_team:{
      type:String,
      default:''
    }
  },
  data() {
    return {
      nameVal: "",
    };
  },
  activated() {
    try {
      //第一次进入首页并带参数basic_info，并符合新人奖条件
      let yz_basic_info = JSON.parse(localStorage.getItem("yz_basic_info"));
      this.info_basic_member_team = yz_basic_info.globalParameter && yz_basic_info.globalParameter.member_team ? yz_basic_info.globalParameter.member_team : "";
    } catch (err) {
      console.log(err);
    }
  },
  methods: {
    cancelTap() {
      this.$emit("childShow", false);
    },
    confirmTap() {
      if (!this.fun.isTextEmpty(this.nameVal)) {
        // 不为空就请求接口
        let url = ''
        if (this.member) {
          // 会员中心是编辑
          url = 'plugin.member-team.frontend.controllers.team.edit'
        } else {
          // 首页的是新增
          url = 'plugin.member-team.frontend.controllers.team.add'
        }
        $http
            .post(url, { team_name: this.nameVal, basic_info: 1 }, " ")
            .then(response => {
              if (response.result === 1) {
                this.$dialog.alert({ message: response.msg });
                this.$emit("childShow", false);
                this.$emit("editTeamName",response.data.name)
              } else {
                this.$emit("childShow", false);
                this.$dialog.alert({ message: response.msg });
              }
            })
            .catch(err => {
              console.error(err, "app");
            });
      }
    }
  }
};
</script>
<style lang="scss" scoped>
#teamName {
  p {
    padding: 1rem 0.625rem;
    text-align: left;
    font-size: 14px;
  }
}
</style>